﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;




namespace NutriFast
{
    public partial class DietasComidas : Form
    {
        public static int idDiet, idMeal;
        public static float Pt, Lip, CHO;
        public float[] vec;
        public decimal a1 = 0, a2 = 0, a3 = 0, a4 = 0, a5 = 0, a6 = 0, a7 = 0, a8 = 0, a9 = 0, a10 = 0, a11 = 0, a12 = 0, a13 = 0, a14 = 0, a15 = 0, a16 = 0, a17 = 0, a18 = 0, a19 = 0;
        public decimal proteinas = 0, lipidos = 0, carbohidratos = 0;
        public bool p = false, l = false, c = false, yes = true;



        DietaSelComida dsc;
        public DietasComidas(DietaSelComida dsl,int D, int M, float P, float L, float C)
        {
            this.dsc = dsl;
            idDiet = D;
            idMeal = M;
            Pt = P;
            Lip = L;
            CHO = C;
           //Pt = 80;
           //Lip = 71;
           //CHO = 158;
            System.Console.WriteLine("idd= "+ D+ "idM= "+ M);

            vec = new float[(int)CHO * 4];
            float aumento = (float)0;
            for (int i = 1; i < (int)CHO * 4; i++)
            {
                vec[i] = aumento;
                aumento = aumento + (float).25;
            }
            
           
            InitializeComponent();
            label87.Text = "" +Pt;
            label88.Text = "" +Lip;
            label89.Text = "" +CHO;

            
        }


            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                int i = 1;
                label26.Text = "" + dbPeri.GetProteina(i) * numericUpDown1.Value;
                label60.Text = "" + dbPeri.GetLipidos(i) * numericUpDown1.Value;
                label78.Text = "" + dbPeri.GetCHO(i) * numericUpDown1.Value;
                suma();
            }
        

        private void numericUpDown2_Click(object sender, EventArgs e)
        {
            int i = 2;
            label25.Text = "" + dbPeri.GetProteina(i) * numericUpDown2.Value;
            label59.Text = "" + dbPeri.GetLipidos(i) * numericUpDown2.Value;
            label77.Text = "" + dbPeri.GetCHO(i) * numericUpDown2.Value;
            suma();
        }

        private void numericUpDown3_Click(object sender, EventArgs e)
        {
            int i = 3;
            label27.Text = "" + dbPeri.GetProteina(i) * numericUpDown3.Value;
            label58.Text = "" + dbPeri.GetLipidos(i) * numericUpDown3.Value;
            label76.Text = "" + dbPeri.GetCHO(i) * numericUpDown3.Value;
            suma();
        }

        private void numericUpDown17_Click(object sender, EventArgs e)
        {
            int i = 17;
            label41.Text = "" + dbPeri.GetProteina(i) * numericUpDown17.Value;
            label44.Text = "" + dbPeri.GetLipidos(i) * numericUpDown17.Value;
            label62.Text = "" + dbPeri.GetCHO(i) * numericUpDown17.Value;
            suma();
        }

        private void numericUpDown16_Click(object sender, EventArgs e)
        {
            int i = 16;
            label40.Text = "" + dbPeri.GetProteina(i) * numericUpDown16.Value;
            label45.Text = "" + dbPeri.GetLipidos(i) * numericUpDown16.Value;
            label63.Text = "" + dbPeri.GetCHO(i) * numericUpDown16.Value;
            suma();
        }

        private void numericUpDown15_Click(object sender, EventArgs e)
        {
            int i = 15;
            label39.Text = "" + dbPeri.GetProteina(i) * numericUpDown15.Value;
            label46.Text = "" + dbPeri.GetLipidos(i) * numericUpDown15.Value;
            label64.Text = "" + dbPeri.GetCHO(i) * numericUpDown15.Value;
            suma();
        }

        private void numericUpDown14_Click(object sender, EventArgs e)
        {
            int i = 14;
            label38.Text = "" + dbPeri.GetProteina(i) * numericUpDown14.Value;
            label47.Text = "" + dbPeri.GetLipidos(i) * numericUpDown14.Value;
            label65.Text = "" + dbPeri.GetCHO(i) * numericUpDown14.Value;
            suma();
        }

        private void numericUpDown13_Click(object sender, EventArgs e)
        {
            int i = 13;
            label37.Text = "" + dbPeri.GetProteina(i) * numericUpDown13.Value;
            label48.Text = "" + dbPeri.GetLipidos(i) * numericUpDown13.Value;
            label66.Text = "" + dbPeri.GetCHO(i) * numericUpDown13.Value;
            suma();
        }

        private void numericUpDown12_Click(object sender, EventArgs e)
        {
            int i = 12;
            label36.Text = "" + dbPeri.GetProteina(i) * numericUpDown12.Value;
            label49.Text = "" + dbPeri.GetLipidos(i) * numericUpDown12.Value;
            label67.Text = "" + dbPeri.GetCHO(i) * numericUpDown12.Value;
            suma();
        }

        private void numericUpDown11_Click(object sender, EventArgs e)
        {
            int i = 11;
            label35.Text = "" + dbPeri.GetProteina(i) * numericUpDown11.Value;
            label50.Text = "" + dbPeri.GetLipidos(i) * numericUpDown11.Value;
            label68.Text = "" + dbPeri.GetCHO(i) * numericUpDown11.Value;
            suma();
        }

        private void numericUpDown10_Click(object sender, EventArgs e)
        {
            int i = 10;
            label34.Text = "" + dbPeri.GetProteina(i) * numericUpDown10.Value;
            label51.Text = "" + dbPeri.GetLipidos(i) * numericUpDown10.Value;
            label69.Text = "" + dbPeri.GetCHO(i) * numericUpDown10.Value;
            suma();
        }

        private void numericUpDown9_Click(object sender, EventArgs e)
        {
            int i = 9;
            label33.Text = "" + dbPeri.GetProteina(i) * numericUpDown9.Value;
            label52.Text = "" + dbPeri.GetLipidos(i) * numericUpDown9.Value;
            label70.Text = "" + dbPeri.GetCHO(i) * numericUpDown9.Value;
            suma();
        }

        private void numericUpDown8_Click(object sender, EventArgs e)
        {
            int i = 8;
            label32.Text = "" + dbPeri.GetProteina(i) * numericUpDown8.Value;
            label53.Text = "" + dbPeri.GetLipidos(i) * numericUpDown8.Value;
            label71.Text = "" + dbPeri.GetCHO(i) * numericUpDown8.Value;
            suma();
        }

        private void numericUpDown7_Click(object sender, EventArgs e)
        {
            int i = 7;
            label31.Text = "" + dbPeri.GetProteina(i) * numericUpDown7.Value;
            label54.Text = "" + dbPeri.GetLipidos(i) * numericUpDown7.Value;
            label72.Text = "" + dbPeri.GetCHO(i) * numericUpDown7.Value;
            suma();
        }

        private void numericUpDown6_Click(object sender, EventArgs e)
        {
            int i = 6;
            label30.Text = "" + dbPeri.GetProteina(i) * numericUpDown6.Value;
            label55.Text = "" + dbPeri.GetLipidos(i) * numericUpDown6.Value;
            label73.Text = "" + dbPeri.GetCHO(i) * numericUpDown6.Value;
            suma();
        }

        private void numericUpDown5_Click(object sender, EventArgs e)
        {
            int i = 5;
            label29.Text = "" + dbPeri.GetProteina(i) * numericUpDown5.Value;
            label56.Text = "" + dbPeri.GetLipidos(i) * numericUpDown5.Value;
            label74.Text = "" + dbPeri.GetCHO(i) * numericUpDown5.Value;
            suma();
        }

        private void numericUpDown4_Click(object sender, EventArgs e)
        {
            int i = 4;
            label28.Text = "" + dbPeri.GetProteina(i) * numericUpDown4.Value;
            label57.Text = "" + dbPeri.GetLipidos(i) * numericUpDown4.Value;
            label75.Text = "" + dbPeri.GetCHO(i) * numericUpDown4.Value;
            suma();
        }

        private void numericUpDown18_Click(object sender, EventArgs e)
        {
            int i = 18;
            label81.Text = "" + dbPeri.GetProteina(i) * numericUpDown18.Value;
            label86.Text = "" + dbPeri.GetLipidos(i) * numericUpDown18.Value;
            label84.Text = "" + dbPeri.GetCHO(i) * numericUpDown18.Value;
            suma();
        }

        private void numericUpDown19_Click(object sender, EventArgs e)
        {

            int i = 19;
            label82.Text = "" + dbPeri.GetProteina(i) * numericUpDown19.Value;
            label83.Text = "" + dbPeri.GetLipidos(i) * numericUpDown19.Value;
            label85.Text = "" + dbPeri.GetCHO(i) * numericUpDown19.Value;
            suma();
        }

        public void suma()
        {
           
            lbt1.Text = "" + (float.Parse(label26.Text) + float.Parse(label25.Text) + float.Parse(label27.Text) + float.Parse(label28.Text) + float.Parse(label29.Text) + float.Parse(label30.Text) + float.Parse(label31.Text) + float.Parse(label32.Text) + float.Parse(label33.Text) + float.Parse(label34.Text) + float.Parse(label35.Text) + float.Parse(label36.Text) + float.Parse(label37.Text) + float.Parse(label38.Text) + float.Parse(label39.Text) + float.Parse(label40.Text) + float.Parse(label41.Text) + float.Parse(label81.Text) + float.Parse(label82.Text));
            lbt2.Text = "" + (float.Parse(label44.Text) + float.Parse(label45.Text) + float.Parse(label46.Text) + float.Parse(label47.Text) + float.Parse(label48.Text) + float.Parse(label49.Text) + float.Parse(label50.Text) + float.Parse(label51.Text) + float.Parse(label52.Text) + float.Parse(label53.Text) + float.Parse(label54.Text) + float.Parse(label55.Text) + float.Parse(label56.Text) + float.Parse(label57.Text) + float.Parse(label58.Text) + float.Parse(label59.Text) + float.Parse(label60.Text) + float.Parse(label86.Text) + float.Parse(label83.Text));
            lbt3.Text = "" + (float.Parse(label62.Text) + float.Parse(label63.Text) + float.Parse(label64.Text) + float.Parse(label65.Text) + float.Parse(label66.Text) + float.Parse(label67.Text) + float.Parse(label68.Text) + float.Parse(label69.Text) + float.Parse(label70.Text) + float.Parse(label71.Text) + float.Parse(label72.Text) + float.Parse(label73.Text) + float.Parse(label74.Text) + float.Parse(label75.Text) + float.Parse(label76.Text) + float.Parse(label77.Text) + float.Parse(label78.Text) + float.Parse(label84.Text) + float.Parse(label85.Text));
            progressBar();
            if ((float.Parse(lbt1.Text) > Pt))
            {
                lbt1.BackColor = Color.Red;
            }
            if ((float.Parse(lbt2.Text) > Lip))
            {
                lbt2.BackColor = Color.Red;
            }
            if ((float.Parse(lbt3.Text) > CHO))
            {
                lbt3.BackColor = Color.Red;
            }
        }

        private void Guardar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show(this, "¿Seguro que desea guardar los cambios en la comida?", "Confirmación", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Console.WriteLine("Comida: "+dsc.IDComida);
                if (dbPeri.UpdateMeal(dsc.IDComida,dbVic.buscarDieta(), (float)numericUpDown1.Value, (float)numericUpDown2.Value, (float)numericUpDown3.Value, (float)numericUpDown4.Value, (float)numericUpDown5.Value, (float)numericUpDown6.Value, (float)numericUpDown7.Value, (float)numericUpDown8.Value, (float)numericUpDown9.Value, (float)numericUpDown10.Value, (float)numericUpDown11.Value, (float)numericUpDown12.Value, (float)numericUpDown13.Value, (float)numericUpDown14.Value, (float)numericUpDown15.Value, (float)numericUpDown16.Value, (float)numericUpDown17.Value, (float)numericUpDown18.Value, (float)numericUpDown19.Value))
                {
                    Console.WriteLine(idDiet);
                    Console.WriteLine(idMeal);
                    this.Dispose();
                }
            }
        }

        private void DietasComidas_Load(object sender, EventArgs e)
        {
            this.Text = "Comida " + idMeal;
        }

        public void progressBar()
        {
             ///// Pt
            progressBar1.Minimum = 0;
            progressBar1.Maximum = (int)Pt;
            if (((int)(float.Parse(lbt1.Text)) < Pt))
            {
                progressBar1.Value = (int)(float.Parse(lbt1.Text));
                progressBar4.Value = 0;
                lbt1.BackColor = System.Drawing.SystemColors.Control;
            }

            else
            {
                progressBar1.Value = (int)Pt;
                progressBar4.Minimum = 0;
                progressBar4.Maximum = (int)Pt;
                if (((int)(float.Parse(lbt1.Text)-Pt) < Pt))
                    progressBar4.Value = (int)(float.Parse(lbt1.Text)-Pt);
                ///////////////////     progressBar4.BackColor = Color.DarkBlue;
            }

            ////LIPIDOS
            progressBar3.Minimum = 0;
            progressBar3.Maximum = (int)Lip;
            if (((int)(float.Parse(lbt2.Text)) < Lip))
            {
                progressBar3.Value = (int)(float.Parse(lbt2.Text));
                progressBar5.Value = 0;
                lbt2.BackColor = System.Drawing.SystemColors.Control;
            }

            else
            {
                progressBar3.Value = (int)Lip;
                progressBar5.Minimum = 0;
                progressBar5.Maximum = (int)Lip;
                if (((int)(float.Parse(lbt2.Text)-Lip) < Lip))
                    progressBar5.Value = (int)(float.Parse(lbt2.Text)-Lip);
            }

            /////   CHO
            progressBar2.Minimum = 0;
            progressBar2.Maximum = (int)CHO;
            if (((int)(float.Parse(lbt3.Text)) < CHO))
            {
                progressBar2.Value = (int)(float.Parse(lbt3.Text));
                progressBar6.Value = 0;
                lbt3.BackColor = System.Drawing.SystemColors.Control;
            }

            else
            {
                progressBar2.Value = (int)CHO;
                progressBar6.Minimum = 0;
                progressBar6.Maximum = (int)CHO;
                if (((int)(float.Parse(lbt3.Text)-CHO) < CHO))
                    progressBar6.Value = (int)(float.Parse(lbt3.Text)-CHO);
            }          
        }

        public int random(int max)
        {
            Random random = new Random();
            return random.Next(1, 12);
        }

        public float vector( int max)
        {
            return vec[max];
            
        }

        public void llenarRandom()
        {
           
            a1=0; a2=0; a3=0;a4=0; a5=0;a6=0;a7=0; a8=0;a9=0;a10=0;a11=0; a12=0;a13=0; a14=0; a15=0;a16=0;a17=0;a18=0;a19=0;
            proteinas=0; lipidos=0; carbohidratos = 0;
            p = false; l = false; c = false; yes = true;
            bool siguele = true;
            int j = 0;

            Calculando calc = new Calculando();

            calc.MdiParent = this.MdiParent;
            calc.Show();
            calc.Focus();
            Application.DoEvents();

            while (siguele)
            {   
                while (yes)
                {

                    p = false;
                    l = false;
                    c = false;
                    Random r = new Random();
                    int i = 0;
                    int y = r.Next(1, 19);

                    switch (y)
                    {
                        case (1):
                            i = r.Next(6, 12);
                            a1 = (decimal)vector(i);
                            break;
                        case (2):
                            i = r.Next(6, 12);
                            a2 = (decimal)vector(i);
                            break;
                        case (3):
                            i = r.Next(6, 12);
                            a3 = (decimal)vector(i);
                            break;
                        case (4):
                            i = r.Next(5, 10);
                            a4 = (decimal)vector(i);
                            break;
                        case (5):
                            i = r.Next(5, 10);
                            a5 = (decimal)vector(i);
                            break;
                        case (6):
                            i = r.Next(5, 10);
                            a6 = (decimal)vector(i);
                            break;
                        case (7):
                            i = r.Next(5, 10);
                            a7 = (decimal)vector(i);
                            break;
                        case (8):
                            i = r.Next(5, 10);
                            a8 = (decimal)vector(i);
                            break;
                        case (9):
                            i = r.Next(5, 10);
                            a9 = (decimal)vector(i);
                            break;
                        case (10):
                            i = r.Next(5, 10);
                            a10 = (decimal)vector(i);
                            break;
                        case (11):
                            i = r.Next(5, 10);
                            a11 = (decimal)vector(i);
                            break;
                        case (12):
                            i = r.Next(5, 10);
                            a12 = (decimal)vector(i);
                            break;
                        case (13):
                            i = r.Next(5, 10);
                            a13 = (decimal)vector(i);
                            break;
                        case (14):
                            i = r.Next(5, 10);
                            a14 = (decimal)vector(i);
                            break;
                        case (15):
                            i = r.Next(5, 10);
                            a15 = (decimal)vector(i);
                            break;
                        case (16):
                            i = r.Next(5, 10);
                            a16 = (decimal)vector(i);
                            break;
                        case (17):
                            i = r.Next(5, 10);
                            a17 = (decimal)vector(i);
                            break;
                        case (18):
                            i = r.Next(5, 10);
                            a18 = (decimal)vector(i);
                            break;
                        case (19):
                            i = r.Next(5, 10);
                            a19 = (decimal)vector(i);
                            break;
                    }


                    proteinas = (a1 * 2) + (a3 * 2) + (a4 * 2) + (a5 * 8) + (a6 * 7) + (a7 * 7) + (a8 * 7) + (a9 * 7) + (a10 * 9) + (a11 * 9) + (a12 * 9) + (a13 * 8) + (a15 * 3);
                    lipidos = (a4 * 5) + (a5 * 1) + (a6 * 1) + (a7 * 3) + (a8 * 5) + (a9 * 8) + (a10 * 2) + (a11 * 4) + (a12 * 8) + (a13 * 5) + (a14 * 5) + (a15 * 5) +(a17 * 5);
                    carbohidratos = (a1 * 4) + (a2 * 15) + (a3 * 15) + (a4 * 15) + (a5 * 20) + (a10 * 12) + (a11 * 12) + (a12 * 12) + (a13 * 30) + (a15 * 3) + (a16 * 10) + (a17 * 10) + (a19 * 20);

                    if ((((float)Pt - (Pt * .3)) < (float)proteinas) && ((float)proteinas) < ((float)Pt + (Pt * .1)))
                        p = true;
                    if ((((float)Lip - (Lip * .3)) < (float)lipidos) && ((float)lipidos) < ((float)Lip + (Lip * .1)))
                        l = true;
                    if ((((float)CHO - (CHO * .3)) < (float)carbohidratos) && ((float)carbohidratos) < ((float)CHO + (CHO * .1)))
                        c = true;
                    if (p && l && c)
                        yes = false;
                    if (j > 200)
                        yes = false;
                }
                if (p && l && c)
                    siguele = false;
                else
                {
                    yes = true;
                    j = 0;
                    a1 = 0; a2 = 0; a3 = 0; a4 = 0; a5 = 0; a6 = 0; a7 = 0; a8 = 0; a9 = 0; a10 = 0; a11 = 0; a12 = 0; a13 = 0; a14 = 0; a15 = 0; a16 = 0; a17 = 0; a18 = 0; a19 = 0;
                }


            }
            calc.Dispose();
            numericUpDown1.Value = a1;
            numericUpDown2.Value = a2;
            numericUpDown3.Value = a3;
            numericUpDown4.Value = a4;
            numericUpDown5.Value = a5;
            numericUpDown6.Value = a6;
            numericUpDown7.Value = a7;
            numericUpDown8.Value = a8;
            numericUpDown9.Value = a9;
            numericUpDown10.Value = a10;
            numericUpDown11.Value = a11;
            numericUpDown12.Value = a12;
            numericUpDown13.Value = a13;
            numericUpDown14.Value = a14;
            numericUpDown15.Value = a15;
            numericUpDown16.Value = a16;
            numericUpDown17.Value = a17;
            numericUpDown18.Value = a18;
            numericUpDown19.Value = a19;
           

        }

        private void button2_Click(object sender, EventArgs e)
        {



           
            llenarRandom();

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            limpiarr();
        }
        public void limpiarr()
        {
            numericUpDown1.Value = (decimal)0;
            numericUpDown2.Value = (decimal)0;
            numericUpDown3.Value = (decimal)0;
            numericUpDown4.Value = (decimal)0;
            numericUpDown5.Value = (decimal)0;
            numericUpDown6.Value = (decimal)0;
            numericUpDown7.Value = (decimal)0;
            numericUpDown8.Value = (decimal)0;
            numericUpDown9.Value = (decimal)0;
            numericUpDown10.Value = (decimal)0;
            numericUpDown11.Value = (decimal)0;
            numericUpDown12.Value = (decimal)0;
            numericUpDown13.Value = (decimal)0;
            numericUpDown14.Value = (decimal)0;
            numericUpDown15.Value = (decimal)0;
            numericUpDown16.Value = (decimal)0;
            numericUpDown17.Value = (decimal)0;
            numericUpDown18.Value = (decimal)0;
            numericUpDown19.Value = (decimal)0;
        }

               
    }
}
